<template>
  <div class="p-2 tsll-content">
    <el-card class="tsll-table__container" shadow="hover">
      <el-table v-loading="loading" :data="systemInfoConfigList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column v-if="false" label="id" align="center" prop="id" />
        <el-table-column label="系统名称" align="center" prop="systemName" />
        <el-table-column label="技术支持" align="center" prop="technicalSupport" />
        <el-table-column label="网页名称" align="center" prop="internetLabTitle" />
        <el-table-column label="logo" align="center" prop="logo">
          <template #default="scope">
            <ImagePreview :width="100" :height="100" :src="scope.row.logoUrl" :preview-src-list="[scope.row.logoUrl]" />
          </template>
        </el-table-column>
        <el-table-column label="背景图片" align="center" prop="bgi">
          <template #default="scope">
            <ImagePreview :width="100" :height="100" :src="scope.row.bgiUrl" :preview-src-list="[scope.row.bgiUrl]" />
          </template>
        </el-table-column>

        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['revenue:meterSystemInfo:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加或修改统系信息对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="800px" append-to-body>
      <el-form ref="meterSystemInfoFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系统名称" prop="systemName">
          <el-input v-model="form.systemName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="网页名称" prop="internetLabTitle">
          <el-input v-model="form.internetLabTitle" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="技术支持" prop="technicalSupport">
          <el-input v-model="form.technicalSupport" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="logo" prop="logo">
          <imageUpload v-model="form.logo" :need-del="false" :limit="1" :is-show-tip="false" />
        </el-form-item>
        <el-form-item label="背景图片" prop="bgi">
          <imageUpload v-model="form.bgi" :need-del="false" :limit="1" :is-show-tip="false" />
        </el-form-item>
        <el-form-item v-if="false" label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="SystemInfoConfig" lang="ts">
import { listSystemInfoConfig, getSystemInfoConfig, updateSystemInfoConfig } from '@/api/application/systemInfoConfig';
import { SystemInfoConfigVO, SystemInfoConfigQuery, SystemInfoConfigForm } from '@/api/application/systemInfoConfig/types';
import { useSystemStore } from '@/store/modules/systemConfig';

const systemStore = useSystemStore();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_normal_disable } = toRefs<any>(proxy?.useDict('sys_normal_disable'));

const systemInfoConfigList = ref<SystemInfoConfigVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const systemInfoConfigFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: SystemInfoConfigForm = {
  id: undefined,
  systemName: undefined,
  technicalSupport: undefined,
  logo: undefined,
  bgi: undefined,
  status: undefined,
  remark: undefined,
  logoUrl: undefined,
  bgiUrl: undefined,
  login: undefined,
  loginUrl: undefined,
  internetLabTitle: undefined
};
const data = reactive<PageData<SystemInfoConfigForm, SystemInfoConfigQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    systemName: undefined,
    technicalSupport: undefined,
    logo: undefined,
    bgi: undefined,
    status: undefined,
    logoUrl: undefined,
    bgiUrl: undefined,
    login: undefined,
    loginUrl: undefined,
    internetLabTitle: undefined,
    params: {}
  },
  rules: {
    systemName: [{ required: true, message: '系统名称不能为空', trigger: 'blur' }],
    technicalSupport: [{ required: true, message: '技术支持不能为空', trigger: 'blur' }],
    logo: [{ required: true, message: 'logo不能为空', trigger: 'blur' }],
    bgi: [{ required: true, message: '背景图片不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
    logoUrl: [{ required: true, message: 'logo不能为空', trigger: 'blur' }],
    bgiUrl: [{ required: true, message: '背景图片不能为空', trigger: 'blur' }],
    login: [{ required: true, message: '登录头像不能为空', trigger: 'blur' }],
    loginUrl: [{ required: true, message: '登录头像url不能为空', trigger: 'blur' }],
    internetLabTitle: [{ required: true, message: '网页浏览器标题不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询系统信息配置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSystemInfoConfig(queryParams.value);
  systemInfoConfigList.value = res.rows;
  total.value = res.total;
  loading.value = false;
  if (res.rows.length == 1) {
    systemStore.setLogoUrl(res.rows[0].logoUrl);
    systemStore.setSystemName(res.rows[0].systemName);
    systemStore.setTitle(res.rows[0].internetLabTitle);
  }
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  systemInfoConfigFormRef.value?.resetFields();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: SystemInfoConfigVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 修改按钮操作 */
const handleUpdate = async (row?: SystemInfoConfigVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getSystemInfoConfig(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改系统信息配置';
};

/** 提交按钮 */
const submitForm = () => {
  systemInfoConfigFormRef.value?.validate(async (valid: boolean) => {
    console.log(valid);
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSystemInfoConfig(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

onMounted(() => {
  getList();
});
</script>
